#!/bin/csh

# -----------------------------------------------------
# Launcher for: Observer Support System Executive. (OSX).
# -----------------------------------------------------

onintr cleanup

source /opt/ngat/etc/cshrc
 
source ${DEPLOY_BIN}/class_setup 
source ${DEPLOY_BIN}/osx_setup

set tel = $DEPLOY_SYSTEM

set DAT = `date -u +"%Y_%j_%H%M"`
set OSS_CONSOLE_LOG_FILE = ${DEPLOY_TMP}/oss_${tel}.log

source $OSS_CONFIG/${tel}_oss_args

$DEPLOY_ETC/log_roll $OSS_CONSOLE_LOG_FILE $OSS_LOG_MAX $OSS_LOG_COUNT


echo "OSX Invokation at $DAT ..."


# Set true if we want DB caching - fast but uses ~50KByte per proposal.
set CACHE_DB = -Dcache.db\=false
#set CACHE_DB = -Dcache.db\=true


# Set this if we want to write the PID.
unset WRITE_PID

# Set to force running logs to be deleted.
set DELETE_LOGS

# Set this to redirect console output - we MUST do this if started by OSW
set REDIRECT_CONSOLE

# Set to force logs to be archived.
set ARCHIVE_LOGS

set PID = ${DEPLOY_TMP}/oss.pid

set IN = $1 


switch ($IN)
    #
    # STOP
    #
    case stop:

    	echo "Observer Support System Executive stopping"  >>& $OSS_CONSOLE_LOG_FILE
	
	if (! -e $PID) then
	    echo "Cannot locate a PID file: $PID "  >>& $OSS_CONSOLE_LOG_FILE
	    exit 1
	endif
	set AID = `cat $PID` 
    
	if ($AID == "") then
	    echo "$PID does not contain a valid PID for the OSS" >>& $OSS_CONSOLE_LOG_FILE
	    exit 2
	endif

	set BID = `ps -ef | grep $AID | awk '{if ($3 == '$AID') print $2}'`

	kill -9 $AID  

	kill -9 $BID

	rm -f $PID

	exit 0

    breaksw
    #
    # MANUAL. 
    #
    case "manual":
	echo "Manual startup, no redirect"
	unset REDIRECT_CONSOLE
	unset DELETE_LOGS
	unset ARCHIVE_LOGS
    breaksw
    #
    # AUTO. Used by /sbin/init during bootup start.
    #
    case "auto":
	echo "Startup from OSW Boot" >>&  $OSS_CONSOLE_LOG_FILE
    breaksw
   
endsw

# Archive logs.
if (${?ARCHIVE_LOGS}) then

    echo "Archiving OSS logs" >>& $OSS_CONSOLE_LOG_FILE

    cd $DEPLOY_LOG
 
    # set list = `glob $OSS_ARCHIVE_LIST `
    set list = `glob oss_sched*.txt `
 
    if ($status == 0) then
	set ct = 0
	set mf = 0
	echo "Checking log files for archiving..." >>& $OSS_CONSOLE_LOG_FILE
	foreach file ( $list )
	    @ ct++
	    mv -v $file tmp/ >>& $OSS_CONSOLE_LOG_FILE
	    if ($status == 0) then
		@ mf++
	    endif
	end
	echo "Moved $mf files of $ct for archiving" >>& $OSS_CONSOLE_LOG_FILE
	if ($mf == $ct ) then
	    echo "Deleting dross..." >>& $OSS_CONSOLE_LOG_FILE	
	    set olist = `glob oss_*.txt `
	    if ($status == 0) then
		/bin/rm -fv oss_*.txt >>& $OSS_CONSOLE_LOG_FILE
	    endif
	endif
    else
	echo "No files to archive, Deleting dross..." >>& $OSS_CONSOLE_LOG_FILE
	set olist = `glob oss_*.txt `
	if ($status == 0) then
	    /bin/rm -fv oss_*.txt >>& $OSS_CONSOLE_LOG_FILE
	endif
    endif

    cd tmp

    echo "Creating archive..." >>& $OSS_CONSOLE_LOG_FILE
    tar cvf Ops_${tel}_oss_${DAT}.tar  oss*.txt  >>& $OSS_CONSOLE_LOG_FILE
    gzip -f Ops_${tel}_oss_${DAT}.tar
    mv -fv  Ops_${tel}_oss_${DAT}.tar.gz $DEPLOY_LOG/archive/

    echo "Deleting raw log files.." >>& $OSS_CONSOLE_LOG_FILE
    set olist = `glob oss_*.txt `
    if ($status == 0) then
	/bin/rm -fv oss_*.txt >>& $OSS_CONSOLE_LOG_FILE
    endif

endif

# Delete running logs.
if (${?DELETE_LOGS}) then
    echo "Deleting running logs" >>&  $OSS_CONSOLE_LOG_FILE
    set olist = `glob $DEPLOY_LOG/oss_*.txt `
    if ($status == 0) then
	/bin/rm -fv $DEPLOY_LOG/oss_*.txt >>& $OSS_CONSOLE_LOG_FILE
    endif
endif
echo "Running logs are history" >>& $OSS_CONSOLE_LOG_FILE

# Loose the lock file
if (-e ${OSS_HOME}/phase2.tmp) then
    echo "Removing OSS DB lock file" >>& $OSS_CONSOLE_LOG_FILE
    rm -f ${OSS_HOME}/phase2.tmp
endif

set APP = ngat.oss.Oss

set ASTRO = -Dastrometry.impl=ngat.astrometry.TestCalculator

# Some startup options.
set SCHED_CACHE = -Dscheduler.cache\=false
set SCHED_LOOK = -Dscheduler.lookahead\=false

# Leave JIT on for speed
#set JIT  = -Xmaxjitcodesize0
#set JIT = -Xint
set JIT

set EXM = "-Dexec.model"

cd $OSS_HOME

set EGD = "-Djava.security.egd=file:/dev/urandom"  
echo "writing PID" >>& $OSS_CONSOLE_LOG_FILE
echo $$ > $PID
echo "Wrote PID "  >>& $OSS_CONSOLE_LOG_FILE

if (${?REDIRECT_CONSOLE}) then   
    $JAVA -DOSS $EGD -Ddb.dir=/occ/oss -Ddb.name=$OSS_DB -Djydodb=$OSS_DIR \
	 $SCHED_CACHE $SCHED_LOOK $CACHE_DB $ASTRO $EXM \
	 $APP -base $OSS_HOME  >>& $OSS_CONSOLE_LOG_FILE	    
    set pexit = $status
else
    $JAVA -DOSS $EGD -Ddb.dir=/occ/oss -Ddb.name=$OSS_DB -Djydodb=$OSS_DIR \
	 $SCHED_CACHE $SCHED_LOOK $CACHE_DB $ASTRO $EXM \
	 $APP -base $OSS_HOME  
    set pexit = $status
endif

cleanup:
echo "Cleaning up"
if (-e ${OSS_HOME}/phase2.tmp) then
    echo "Removing OSS DB lock file" >& $OSS_CONSOLE_LOG_FILE
    rm -f ${OSS_HOME}/phase2.tmp
endif 
